<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(isset($is_guest) || isset($is_user) || isset($is_moderator))
{ header("location: $PHP_SELF?i=2"); exit; }

//-----------------------------------------------------------------
//  Set Variables
//-----------------------------------------------------------------

if(!isset($d)) $d = "./";
if(!isset($dircount)) $dircount = 0;
if(!isset($layout1)) $layout1 = NULL;
if(!isset($layout2)) $layout2 = NULL;
if(!isset($filecount)) $filecount = 0;
if(!isset($traceback)) $traceback = NULL;

//-----------------------------------------------------------------
//  Begin Switch
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  View File
	//-----------------------------------------------------------------
	case 1:
	
		//-----------------------------------------------------------------
		//  Do Traceback
		//-----------------------------------------------------------------
		
		if($d == "./")
		{
			$traceback = "Browse";
		}
		else
		{
			$traceback .= "<a href='$PHP_SELF?n=pages/browse'>Browse</a> &raquo; ";
			
			$bread_t = explode('/', trim($d,'/'));
			for($bread_i = 0, $bread_r = array(), $bread_z = ''; ($bread_r[] = @$bread_t[$bread_i]), $bread_z= @$bread_t[$bread_i]; $bread_i++)
			{		
				if(implode('/', $bread_r) !== trim($d, '/')) $traceback .= "<a href='$PHP_SELF?n=pages/browse&d=".implode('%2F',$bread_r)."'>".ucfirst(str_replace("_", " ", $bread_z))."</a> &raquo; ";
				else $traceback .= ucfirst(str_replace("_", " ", $bread_z));
			}
		}
		
		//-----------------------------------------------------------------
		//  Open File
		//-----------------------------------------------------------------
		
		$file = implode("", file("$d"));
		
		//-----------------------------------------------------------------
		//  Layout
		//-----------------------------------------------------------------
	?>
		<table class='main' cellpadding='4' cellspacing='1'>
		<tr class='con1'><td><?= $traceback ?></td></tr>
		</table>
		
		<table><tr><td></td></tr></table>	
		
		<form method='post' action='<?= $PHP_SELF ?>?n=pages/browse&a=2&d=<?= $d ?>'>
		<table class='main' cellpadding='4' cellspacing='1'>
		<tr class='head'><td>Edit File</td></tr>
		<tr class='con1'><td><textarea class='box' name='script' style='width:100%;' rows='20'><?= $file ?></textarea></td></tr>
		</table>
		
		<table><tr><td></td></tr></table>	
		
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class='con1'>
		<td width="50%">Actions:</td>
		<td width="50%" align="right"><input type='submit' name='submit' style='width:50%' class='box' value='Save' tabindex='3'></td>
		</tr>
		</table>
		</form>
	<?
	break;
	
	//-----------------------------------------------------------------
	//  Edit File Proccess
	//-----------------------------------------------------------------
	case 2:
	
		//-----------------------------------------------------------------
		//  Do Checks
		//-----------------------------------------------------------------
		
		if(!is_writable($d)) { header("location: $PHP_SELF?n=pages/browse&a=1&d=$d&i=150"); exit; }
		if(!$handle = fopen($d, 'w')) { header("location: $PHP_SELF?n=pages/browse&a=1&d=$d&i=151"); exit; }
		if(fwrite($handle, $script) === FALSE) { header("location: $PHP_SELF?n=pages/browse&a=1&d=$d&i=152"); exit; }
		
		//-----------------------------------------------------------------
		//  Close
		//-----------------------------------------------------------------
		
		fclose($handle);
		
		//-----------------------------------------------------------------
		//  Redirect
		//-----------------------------------------------------------------
		
		header("location: $PHP_SELF?n=pages/browse&a=1&d=$d&i=153");
		
	break;

	//-----------------------------------------------------------------
	//  Show Files and Dirs
	//-----------------------------------------------------------------
	default:
	
		//-----------------------------------------------------------------
		//  Get Directories and Files
		//-----------------------------------------------------------------
		
		if($handle = opendir($d))
		{
			while(false !== ($file = readdir($handle)))
			{
				if ($file != "." && $file != ".." && $file != ".htaccess")
				{
					if(is_dir("$d/$file"))
					{
						if($d == './') $prefix = NULL;
						else $prefix = "$d/";
						
						$data = dirinfo("$d/$file");
						
						$layout1 .= "<tr class='con1'><td><img src='images/browse/folder.png' /></td><td colspan='3'><a href='$PHP_SELF?n=pages/browse&d=$prefix$file'>$file</a></td><td>".format_size($data['size'])."</td><td>{$data['dirs']}</td><td>{$data['files']}</td></tr>";
						$dircount++;
					}
					else
					{
						if($d == './') $prefix = NULL;
						else $prefix = "$d/";
						
						if($prefix != "pages" && $file != "browse.php")
						{
							
							if(getextension("$prefix$file") == "png") $image = "image";
							elseif(getextension("$prefix$file") == "gif") $image = "image";
							elseif(getextension("$prefix$file") == "jpg") $image = "image";
							elseif(getextension("$prefix$file") == "php") $image = "script";
							elseif(getextension("$prefix$file") == "js") $image = "script";
							elseif(getextension("$prefix$file") == "htm") $image = "text";
							elseif(getextension("$prefix$file") == "html") $image = "text";
							elseif(getextension("$prefix$file") == "xhtml") $image = "text";
							elseif(getextension("$prefix$file") == "css") $image = "text";
							elseif(getextension("$prefix$file") == "txt") $image = "text";
							elseif(getextension("$prefix$file") == "rar") $image = "archive";
							elseif(getextension("$prefix$file") == "zip") $image = "archive";
							elseif(getextension("$prefix$file") == "gzip") $image = "archive";
							else $image = "unknown";
							
							if($image == "script" || $image == "text") $link = "$PHP_SELF?n=pages/browse&a=1&d=$prefix$file";
							else $link = "$prefix$file";
							
							$layout2 .= "<tr class='con2'><td><img src='images/browse/$image.png' /></td><td colspan='3'><a href='$link'>".getfilename($file)."</a></td><td>".getextension($file)."</td><td colspan='2'>".format_size(filesize("$prefix$file"))."</td></tr>";
							
							$filecount++;
						}
					}
				}
			}
			closedir($handle);
		}
		
		//-----------------------------------------------------------------
		//  Take Care of things
		//-----------------------------------------------------------------
		
		if(!$layout1) $layout1 .= "<tr class='con1'><td colspan='7'>No Directories</td></tr>";
		if(!$layout2) $layout2 .= "<tr class='con2'><td colspan='7'>No Files</td></tr>";
		
		//-----------------------------------------------------------------
		//  Do Traceback
		//-----------------------------------------------------------------
		
		if($d == "./")
		{
			$traceback = "Browse";
		}
		else
		{
			$traceback .= "<a href='$PHP_SELF?n=pages/browse'>Browse</a> &raquo; ";
			
			$bread_t = explode('/', trim($d,'/'));
			for($bread_i = 0, $bread_r = array(), $bread_z = ''; ($bread_r[] = @$bread_t[$bread_i]), $bread_z= @$bread_t[$bread_i]; $bread_i++)
			{		
				if(implode('/', $bread_r) !== trim($d, '/')) $traceback .= "<a href='$PHP_SELF?n=pages/browse&d=".implode('%2F',$bread_r)."'>".ucfirst(str_replace("_", " ", $bread_z))."</a> &raquo; ";
				else $traceback .= ucfirst(str_replace("_", " ", $bread_z));
			}
		}
		
		?>
		<table class='main' cellpadding='4' cellspacing='1'>
		<tr class='con1'><td><?= $traceback ?></td></tr>
		</table>
		
		<table><tr><td></td></tr></table>
		
		<table class='main' cellpadding='4' cellspacing='1'>
		<col width='10px'><col width='10px'><col width='10px'><col width='10px'>
		<tr class='head'><td colspan='7'>Browse</td></tr>
		<tr class='bottom'><td align='center'>#</td><td colspan='3'>Directory</td><td>Size</td><td>Directories</td><td>Files</td></tr>
		<?= $layout1 ?>
		<tr class='bottom'><td align='center'>#</td><td colspan='3'>File Name</td><td>Type</td><td colspan='2'>Size</td></tr>
		<?= $layout2 ?>
		</table>
		
		<table><tr><td></td></tr></table>
		
		<table class='main' cellpadding='4' cellspacing='1'>
		<col width='50%'><col width='50%'>
		<tr class='head'><td colspan='2'>Directory Infomation</td></tr>
		<tr class='con1'><td>Directories: <?= $dircount ?></td><td>Files <?= $filecount ?></td></tr>
		</table>
		
		<table><tr><td></td></tr></table>
		
		<table class='main' cellpadding='4' cellspacing='1'>
		<tr class='head'><td>Key</td></tr>
		<tr class='con1'><td>
		<table class='main' cellpadding='4' cellspacing='1'>
		<tr class='empty'><td width='1px'><img src='images/browse/folder.png' /></td><td> Directory</td><td width='1px'><img src='images/browse/text.png' /></td><td> Text File</td></tr>
		<tr class='empty'><td width='1px'><img src='images/browse/script.png' /></td><td> PHP Script</td><td width='1px'><img src='images/browse/image.png' /></td><td> Image (gif,png,jpg)</td></tr>
		<tr class='empty'><td width='1px'><img src='images/browse/archive.png' /></td><td> Archive (zip,rar)</td><td width='1px'><img src='images/browse/unknown.png' /></td><td> Unknown</td></tr>
		</table>
		</td></tr>
		</table>
<?
	break;
	}
?>